package com.csx.midware.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.SetParams;

/**
 * Redis 存放登录用户的Session
 */
public class RedisSessionExample {

    private Jedis jedis;

    public RedisSessionExample() {
        jedis = new Jedis("localhost");
    }

    public void setSession(String sessionId, String sessionData) {
        SetParams params = SetParams.setParams().ex(1800); // 设置过期时间为30分钟
        jedis.set(sessionId, sessionData, params);
    }

    public String getSession(String sessionId) {
        return jedis.get(sessionId);
    }

    public void deleteSession(String sessionId) {
        jedis.del(sessionId);
    }

    public static void main(String[] args) {
        RedisSessionExample example = new RedisSessionExample();
        example.setSession("session123", "User data");
        System.out.println("Session Data: " + example.getSession("session123"));
        example.deleteSession("session123");
        System.out.println("Session Data after deletion: " + example.getSession("session123"));
    }
}